package cn.wd.service.impl;

import cn.wd.domain.Employee;
import cn.wd.mapper.EmployeeMapper;
import cn.wd.page.PageResult;
import cn.wd.qo.QueryObject;
import cn.wd.service.IEmployeeService;
import cn.wd.utils.TokenManager;
import cn.wd.vo.BatchData;
import cn.wd.vo.LoginInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private final EmployeeMapper mapper;
    public EmployeeServiceImpl(EmployeeMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public String login(String username, String password) {

        // 1. 基于用户名去数据库查询用户信息
        Employee employee = mapper.selectByEmployeeName(username);

        // 2. 如果信息不存在, 提示用户名或密码错误
        if (employee == null) {
            log.error("[员工服务] 登录失败, 用户名输入错误, 用户名={}, 提交的密码={}", username, password);
            throw new RuntimeException("用户名或密码错误");
        }

        // 3. 判断密码是否正确, 如果不正确, 提示用户名或密码错误
        if (!password.equals(employee.getPassword())) {
            log.error("[员工服务] 登录失败, 密码输入错误, 用户名={}, 提交的密码={}", username, password);
            throw new RuntimeException("用户名或密码错误");
        }

        // 4. 生成 token, 并将当前用户存入 map
        String token = TokenManager.getToken();

        LoginInfo loginInfo = new LoginInfo();
        // 将 employee 中所有与 loginInfo 对象中属性名相同的属性值全部 copy 到 loginInfo
        BeanUtils.copyProperties(employee, loginInfo);

        TokenManager.put(token, loginInfo);

        // 5. 返回 token
        return token;
    }
















    @Override
    public void save(Employee obj) {
        mapper.insert(obj);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateById(Employee obj) {
        mapper.updateByPrimaryKey(obj);
    }

    @Override
    public Employee selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public PageResult<Employee> query(QueryObject qo) {
        Long total = mapper.selectForCount(qo);
        if (total == 0) {
            return PageResult.empty(qo.getCurrentPage(), qo.getPageSize());
        }
        List<Employee> employeeList = mapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), total, employeeList);
    }


    @Override
    public List<Employee> selectByDepartmentPrimaryKey(Long deptId) {
        return mapper.selectByDepartmentPrimaryKey(deptId);
    }

    /**
     * 员工重置密码
     * @param id 员工 id
     * @param newPassword 原始密码
     * @param oldPassword 新密码
     */
    @Override
    public void restPassword(Long id, String newPassword, String oldPassword) {
        Employee employee = mapper.selectByPrimaryKey(id);
        if (! oldPassword.equals(employee.getPassword())) {
            log.error("[员工重置密码业务]： 员工输入的密码错误，");
            throw new RuntimeException("原始密码错误，请重新输入！");
        }
        mapper.restPassword(id, newPassword);
    }

    /**
     * 员工登录 修改用户信息中的更换头像操作
     * @param token 员工登录的token
     * @param avatar 员工新头像
     */
    @Override
    public void updateAvatar(String token, String avatar) {
        LoginInfo info = TokenManager.get(token);
        if (info != null) {
            mapper.updateAvatar(info.getId(), avatar);
        }
    }

    @Override
    public void batchList(BatchData batchData) {
        mapper.updateEmployeeDeptId(batchData.getDeptId(), batchData.getEmpIds());
    }
}
